﻿using AutoMapper;
using MyCompany.MyProject.IService;
using MyCompany.MyProject.Model;
using MyCompany.MyProject.Repository.Base;
using SqlSugar;
using System.Linq.Expressions;

namespace MyCompany.MyProject.Service;

public class BaseService<TEntity> : IBaseService<TEntity> where TEntity : class, new()
{
    //private readonly IMapper _mapper;
    private readonly IBaseRepository<TEntity> _baseRepository;
    public ISqlSugarClient Db => _baseRepository.Db;

    public BaseService(/*IMapper mapper, */IBaseRepository<TEntity> baseRepository)
    {
        //_mapper = mapper;
        _baseRepository = baseRepository;
    }
    public async Task<List<TEntity>> Query(Expression<Func<TEntity, bool>>? whereExpression = null)
    {
        var entities = await _baseRepository.Query(whereExpression);
        //var mapedList = _mapper.Map<List<TEntity>>(entities);
        //Console.WriteLine($"_baseRepository 实例HashCode ： {_baseRepository.GetHashCode()}");
        return entities;
    }

    public async Task<long> Add(TEntity entity)
    {
        //var mapedEntity = _mapper.Map<TEntity>(entity);
        return await _baseRepository.Add(entity);
    }

    public async Task<List<TEntity>> QuerySplit(Expression<Func<TEntity, bool>> whereExpression, string orderBy = "")
    {
        var res = await _baseRepository.QuerySplit(whereExpression, orderBy);
        //var llout = _mapper.Map<List<TEntity>>(res);
        return res;
    }

    public async Task<List<long>> AddSplit(TEntity entity)
    {
        //var mapedEntity = _mapper.Map<TEntity>(entity);
        return await _baseRepository.AddSplit(entity);
    }

    public async Task<List<TEntity>> QueryWithCache(Expression<Func<TEntity, bool>>? whereExpression = null)
    {
        var entities = await _baseRepository.QueryWithCache(whereExpression);
        //var llout = _mapper.Map<List<TEntity>>(entities);
        return entities;
    }

    public async Task<TEntity> QueryById(object id)
    {
        var model = await _baseRepository.QueryById(id);
        //var modelView = _mapper.Map<TEntity>(model);
        return model;
    }

    public async Task<PageModel<TEntity>> QueryPage(Expression<Func<TEntity, bool>>? whereExpression = null, int pageIndex = 1, int pageSize = 10, string? orderFields = null)
    {
        var res = await _baseRepository.QueryPage(whereExpression, pageIndex, pageSize, orderFields);
        return res;
    }

    public async Task<bool> DeleteByIds(object[] ids)
    {
        return await _baseRepository.DeleteByIds(ids);
    }

    public async Task<List<long>> Add(List<TEntity> entities)
    {
        //var mapedList = _mapper.Map<List<TEntity>>(entities);
        return await _baseRepository.Add(entities);
    }

    public async Task<bool> Update(TEntity entity)
    {
        //var mapedEntity = _mapper.Map<TEntity>(entity);
        return await _baseRepository.Update(entity);
    }

    public async Task<List<TEntity>> QueryByIDs(long[] lstIds)
    {
        var list = await _baseRepository.QueryByIDs(lstIds);
        //var modelList = _mapper.Map<TEntity>(list);
        return list; //_mapper.Map<List<TEntity>>(list);
    }

    public async Task<bool> Update(List<TEntity> model)
    {
        //var mapedList = _mapper.Map<List<TEntity>>(model);
        return await _baseRepository.Update(model);
    }

    public async Task<TEntity> QueryById(object id, bool blnUseCache = false)
    {
        var res = await _baseRepository.QueryById(id, blnUseCache);
        return res; // _mapper.Map<TEntity>(res);
    }
}
